var isSigned = true;
var isSubscribed = false;
var email;

window.onload = function() {
    $('#check-send-email').attr('checked', isSubscribed);
    if (!isSigned) {
        $('#check-send-email_text').hide();
        if (isSubscribed) {
            $('#check-send-email_text').show();
            $('#email').val(email);
            $('#email').attr('disabled', true);
        }
    } else {
        $('#check-send-email_text').show();
    }
};

function sendMailClicked(id) {
    var checked = $("#check-send-email").is(':checked');
    var dataString = 'order=' + id + 
            '&subscribed=' + checked;
    if (checked) {
        $('#email').attr('disabled', true);
    } else {
        $('#email').attr('disabled', false);
    }
    if (!isSigned) {
        dataString += '&email=' + $('#email').val();
    }
    $.ajax({
        type: "POST",
        url: "ViewOrder",
        data: dataString,
        success: function(data) {
        }
    });
}



$(document).ready(function() {
    
    $("#email").keyup(function() {
        if (!validate($("#email").val(), "(([A-Za-z])|(\\d)|([-_.]))+@([A-Za-z])+[.]([A-Za-z]){2,4}")) {
            $("#check-send-email_text").hide();
        } else {
            $("#check-send-email_text").show();
        }
    });
    
    function validate(value, pattern) {
        var reg = new RegExp(pattern);
        return reg.test(value);
    }
});